package dao

import (
	"ssims/models"
)

func GetAllFamily(family map[string]interface{}) (error, []models.Family, int64) {
	var familyData []models.Family
	page := family["page"].(int)
	pageSize := family["limit"].(int)
	searchName := family["searchName"].(string)
	var total int64
	err := db.Table("family").Select("family_id", "family_name", "people_id", "family_sex", "family_birth", "family_die").Where("family_name like ? and is_del = false", searchName+"%").Order("family_id ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&familyData).Error
	return err, familyData, total
}

func DelFamily(id string) error {
	err := db.Table("family").Where("family_id = ?", id).Update("is_del", true).Error
	return err
}

func UpdateFamily(family models.Family) error {
	err := db.Table("family").Select("family_id", "family_name", "people_id", "family_sex", "family_birth", "family_die").Where("family_name = ?", family.FamilyName).Updates(&family).Error
	return err
}

func AddFamily(family models.Family) error {
	err := db.Table("family").Select("family_id", "family_name", "people_id", "family_sex", "family_birth", "family_die", "is_del").Create(&family).Error
	return err
}
